package com.winit.openapi.wh.validator;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.inbound.vo.InboundEnumConstant;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

/**
 * 海外仓入库单 校验器类
 * 
 * @version <pre>
 * Author     Version       Date        Changes
 * dinghua.an   1.0         2015年11月10日     Created
 *
 * </pre>
 * @since 1.
 */
public class InboundOrderfieldValidator {

    /**
     * 校验创建海外仓入库单参数 第一步：基本信息校验 继续（校验字段是否必填） 校验是否为空
     * 
     * @param json
     */
    public static void validFirstStepField(JSONObject json) {
        validInboundOrderField(json);
    }

    /**
     * 校验创建海外仓入库单参数保存到草稿
     * 校验字段长度
     * @param json
     */
    public static void validSaveAsDraftField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "orderNo", 64);
        
        ValidateUtil.validMaxLength(json, "orderType", 20);
        
        ValidateUtil.validMaxLength(json, "unitName", 20);
        ValidateUtil.validMaxLength(json, "importerCode", 50);
        
        ValidateUtil.validMaxLength(json, "exporterCode", 50);
        ValidateUtil.validMaxLength(json, "logisticsPlanNo", 50);
        
    }
    
    public static void validUnpickupOrderField(JSONObject json) {
        /*
         * 获取入库方式，根据入库方式，校验不同字段 基本：描述 标准订单：物流，提货验货配置校验 直发国内验：提货验货配置校验 直发海外：无
         */
        ValidateUtil.validMaxLength(json, "desc", 255, false);
        // 入库单
        ValidateUtil.validNotNull(json, "orderNo");
        ValidateUtil.validNotNull(json, "entryWhType");
        String entryWhType = (String) json.get("entryWhType");
        if (InboundEnumConstant.OrderType.STANDARD.getType().equals(entryWhType) 
        		|| InboundEnumConstant.OrderType.DIRECT_INNER.getType().equals(entryWhType) 
                ||InboundEnumConstant.OrderType.STORAGE_INBOUND.getType().equals(entryWhType)) {
            ValidateUtil.validNotNull(json, "logistics");
            JSONObject logisticsJson = json.getJSONObject("logistics");
            if (logisticsJson != null) {
                // 进口商
                ValidateUtil.validMaxLength(json, "importerCode", 20, false);
                // 出口商
                ValidateUtil.validMaxLength(json, "exporterCode", 20, false);
                // 物流计划
                ValidateUtil.validMaxLength(json, "logisticsPlanNo", 50, true);
            }
        }
        if (!InboundEnumConstant.OrderType.DIRECT_WH.getType().equals(entryWhType)) {
            JSONObject dispatchInfoJson = json.getJSONObject("dispatchInfo");
            if (dispatchInfoJson != null) {
                // 验证提货信息
                InboundPickupFieldValidator.validInspectionPickUp(dispatchInfoJson);
            }
        }
        
        if (InboundEnumConstant.OrderType.DIRECT_INNER.getType().equals(entryWhType)
            || InboundEnumConstant.OrderType.DIRECT_WH.getType().equals(entryWhType)) {
            if ("Winit".equals(json.getString("isContainLogisticsSu"))) {
                ValidateUtil.validNotNull(json, "sendPort");
                JSONObject sendPortJson = json.getJSONObject("sendPort");
                if (sendPortJson != null) {
                    ValidateUtil.validNotNull(sendPortJson, "sendPortType");
                }
            }
        }
    }
    
    /**
     * 
     * 标准海外仓入库单订单参数校验
     * @param order
     */
    public static void validInboundOrder(JSONObject json){
        validInboundOrderField(json);
        InboundPackagefieldValidator.validCreatePackageField(json);
    }
    
    /**
     * 
     * 根据调用模块校验订单信息
     * 1、卖家网站基本信息模块---base
     * 2、卖家网站跨国物流信息模块---logistics
     * 3、API下入库单
     * @param json
     */
    private static void validInboundOrderField(JSONObject json) {
        ValidateUtil.validNotNull(json, "orderType");
//        ValidateUtil.validNotNull(json, "unitName");
        String unitName = json.getString("unitName");
        if (InboundEnumConstant.OrderBusinessType.LOGISTICS.getType().equals(unitName)) {
            /**********卖家网站下单，物流信息接口校验****************************/
            // 入库单
            ValidateUtil.validNotNull(json, "orderNo");
            // 进口商
            ValidateUtil.validMaxLength(json, "importerCode", 20, false);
            // 出口商
            ValidateUtil.validMaxLength(json, "exporterCode", 20, false);
            // 物流计划
            ValidateUtil.validMaxLength(json, "logisticsPlanNo", 50, true);
        } else if (InboundEnumConstant.OrderBusinessType.BASE.getType().equals(unitName)) {
            /**********卖家网站下单，基础信息接口校验****************************/
            String orderType = json.getString("orderType");
            
            //ValidateUtil.validNotNull(json, "isFinance");
            ValidateUtil.validMaxLength(json, "isFinance", 2, true);
            //金融入库单 贷款单号必填
            if ("Y".equals(json.getString("isFinance"))) {
                    ValidateUtil.validNotNull(json, "loanNo");
                    ValidateUtil.validMaxLength(json, "loanNo", 64, true);
                }
            // 标准和直发国内需要验货仓：验货仓
            if (InboundEnumConstant.OrderType.DIRECT_INNER.getType().equals(orderType)
                || InboundEnumConstant.OrderType.STANDARD.getType().equals(orderType)
                || InboundEnumConstant.OrderType.STORAGE_INBOUND.getType().equals(orderType)) {
                
                ValidateUtil.validMaxLength(json, "warehouseCode", 40, true);
            }
            // 目的仓
            ValidateUtil.validMaxLength(json, "destinationWarehouseCode", 40, true);
            // winit产品
            ValidateUtil.validMaxLength(json, "productCode", 32, true);
            // 客户订单号
            ValidateUtil.validMaxLength(json, "customerOrderNo", 255);
        } else {
            /**********API下单，所有入参校验****************************/
            validateInboundOrderByType(json);
        }
    }
    
    /**
     * 
     * 根据订单类型校验:
     * 1、基本信息：winit产品、验货仓、目的仓、卖家订单号
     * 2、跨国物流信息：进出口商、物流计划
     * 3、提货/验货信息：验货方式、提货类型、发货供应商编码/名称、快递单号
     * @param json
     */
    private static void validateInboundOrderByType(JSONObject json){
        String orderType = json.getString("orderType");
        if(InboundEnumConstant.OrderType.STANDARD.getType().equals(orderType)
           ||InboundEnumConstant.OrderType.STORAGE_INBOUND.getType().equals(orderType)){
            //基本信息
            ValidateUtil.validMaxLength(json, "warehouseCode", 40, true);//验货仓
            ValidateUtil.validMaxLength(json, "destinationWarehouseCode", 40, true);//目的仓
            ValidateUtil.validMaxLength(json, "productCode", 40, true);//产品编码
            ValidateUtil.validMaxLength(json, "customerOrderNo", 255);//客户订单号
            //物流信息
            ValidateUtil.validMaxLength(json, "importerCode", 20, false);// 进口商
            ValidateUtil.validInteger(json, "importerCode", false);// 整型
            ValidateUtil.validMaxLength(json, "exporterCode", 20, false);// 出口商
            ValidateUtil.validInteger(json, "exporterCode", false);// 整型
            ValidateUtil.validMaxLength(json, "logisticsPlanNo", 50, true);// 物流计划
            //提货验货信息
            ValidateUtil.validNotNull(json, "dispatchInfo");
            InboundPickupFieldValidator.validInspectionPickUp(json.getJSONObject("dispatchInfo"));
            
        } else if (InboundEnumConstant.OrderType.DIRECT_INNER.getType().equals(orderType)){
            //直发国内验  没有跨国物流信息
            ValidateUtil.validMaxLength(json, "warehouseCode", 40, true);//验货仓
            
            ValidateUtil.validNotNull(json, "dispatchInfo");
            InboundPickupFieldValidator.validInspectionPickUp(json.getJSONObject("dispatchInfo"));
        } else if (InboundEnumConstant.OrderType.DIRECT_WH.getType().equals(orderType)){
            //直发海外验   没有跨国物流、提货信息
        } else if (InboundEnumConstant.OrderType.LOCATION_INBOUND.getType().equals(orderType)) {
            //本地仓入库  没有跨国物流信息
            ValidateUtil.validMaxLength(json, "warehouseCode", 40, true);//验货仓
            
            ValidateUtil.validNotNull(json, "dispatchInfo");
            InboundPickupFieldValidator.validInspectionPickUp(json.getJSONObject("dispatchInfo"));
        } else {
            throw new ApiException("不支持的订单类型!");
        }
        ValidateUtil.validMaxLength(json, "productCode", 32, true);// winit产品
        ValidateUtil.validMaxLength(json, "destinationWarehouseCode", 40, true);//目的仓
        ValidateUtil.validMaxLength(json, "customerOrderNo", 255);// 客户订单号
        
    }
}
